Java Technologies Inheritance Hierarchies এবং Type Compatibility গাইড ও নোট

370

জাভার জেনেরিক্সের ক্ষেত্রে Inheritance Hierarchies এবং Type Compatibility গুরুত্বপূর্ণ ভূমিকা পালন করে। জেনেরিক্স ব্যবহার করার সময় টাইপ ইনহেরিটেন্স এবং কম্প্যাটিবিলিটি পরিচালনা করতে হলে কিছু নিয়ম এবং সীমাবদ্ধতা জানা প্রয়োজন।


Inheritance Hierarchies

জাভায় জেনেরিক ক্লাস এবং ইন্টারফেসের ক্ষেত্রেও ইনহেরিটেন্স নিয়ম প্রযোজ্য।

উদাহরণ: জেনেরিক ক্লাসে ইনহেরিটেন্স

class Parent<T> {
    T value;

    public Parent(T value) {
        this.value = value;
    }

    public T getValue() {
        return value;
    }
}

class Child<T> extends Parent<T> {
    public Child(T value) {
        super(value);
    }

    public void display() {
        System.out.println("Value: " + getValue());
    }
}

ব্যবহারের উদাহরণ:

public class Main {
    public static void main(String[] args) {
        Child<String> child = new Child<>("Java Generics");
        child.display(); // Output: Value: Java Generics
    }
}

Type Compatibility

জেনেরিক টাইপের ইনহেরিটেন্স সাধারণ ক্লাসের মতো সরাসরি কাজ করে না। জেনেরিক্স ব্যবহার করলে টাইপ ইনহেরিটেন্স এবং কম্প্যাটিবিলিটির নিয়মগুলো আরও কঠোর।

উদাহরণ: ইনহেরিটেন্স এবং টাইপ কম্প্যাটিবিলিটি

import java.util.ArrayList;
import java.util.List;

public class CompatibilityExample {
    public static void main(String[] args) {
        List<Object> objectList = new ArrayList<>();
        List<String> stringList = new ArrayList<>();

        // Compile-time error: incompatible types
        // objectList = stringList;

        // Correct: Using wildcards
        List<? extends Object> wildcardList = stringList;

        System.out.println("Compatibility with wildcards works!");
    }
}

কারণ:

  • List<Object> এবং List<String> ইনহেরিটেন্স সম্পর্কিত নয়।
  • Wildcards ব্যবহার করে টাইপ কম্প্যাটিবিলিটি অর্জন করা যায়।

Wildcards এবং ইনহেরিটেন্স

1. Upper-Bounded Wildcards

public void processList(List<? extends Number> list) {
    for (Number num : list) {
        System.out.println(num);
    }
}
  • ব্যবহার: List<Integer>, List<Double> ইত্যাদি পাঠানো যাবে।

2. Lower-Bounded Wildcards

public void addNumbers(List<? super Integer> list) {
    list.add(100); // Adding elements is allowed
}
  • ব্যবহার: List<Number> বা List<Object> পাঠানো যাবে।

জেনেরিক ক্লাস ইনহেরিটেন্সের সময় টাইপ কম্প্যাটিবিলিটি

উদাহরণ:

class GenericParent<T> {
    public void display(T value) {
        System.out.println("Parent: " + value);
    }
}

class GenericChild<T> extends GenericParent<T> {
    @Override
    public void display(T value) {
        System.out.println("Child: " + value);
    }
}

ব্যবহারের উদাহরণ:

public class Main {
    public static void main(String[] args) {
        GenericParent<String> parent = new GenericParent<>();
        parent.display("Hello");

        GenericChild<String> child = new GenericChild<>();
        child.display("World");

        // Using parent reference to child object
        GenericParent<String> parentRef = new GenericChild<>();
        parentRef.display("Inheritance with Generics");
    }
}

আউটপুট:

Parent: Hello
Child: World
Child: Inheritance with Generics

Raw Types এবং Type Compatibility

Raw Type ব্যবহার করলে টাইপ কম্প্যাটিবিলিটির সমস্যা তৈরি হতে পারে। উদাহরণ:

List rawList = new ArrayList();
rawList.add("Hello");
rawList.add(100); // Mixed types allowed

for (Object obj : rawList) {
    System.out.println(obj); // No type safety
}

Bounded Type Parameters এবং ইনহেরিটেন্স

উদাহরণ:

class BoundedParent<T extends Number> {
    public void show(T value) {
        System.out.println("Value: " + value);
    }
}

class BoundedChild<T extends Integer> extends BoundedParent<T> {
    @Override
    public void show(T value) {
        System.out.println("Integer Value: " + value);
    }
}

ব্যবহারের উদাহরণ:

public class Main {
    public static void main(String[] args) {
        BoundedChild<Integer> child = new BoundedChild<>();
        child.show(123); // Output: Integer Value: 123
    }
}

Key Differences Between Generics and Non-Generics Inheritance

AspectNon-GenericsGenerics
Type SafetyNo type safetyCompile-time type safety
Inheritance CompatibilityDirectly compatibleRequires wildcards or explicit typing
FlexibilityHigh but prone to runtime errorsLimited but safe

  1. জেনেরিক্স টাইপ ইনহেরিটেন্সের সময় টাইপ সেফটি নিশ্চিত করে।
  2. টাইপ কম্প্যাটিবিলিটি নিশ্চিত করার জন্য Wildcards (?) অপরিহার্য।
  3. ইনহেরিটেন্সের ক্ষেত্রে সঠিক টাইপ ব্যবহারে টাইপ-সেফ এবং পুনঃব্যবহারযোগ্য কোড তৈরি করা যায়।
  4. Raw Types ব্যবহার এড়ানো উচিত, কারণ এটি টাইপ কম্প্যাটিবিলিটির সমস্যা তৈরি করতে পারে।
Content added By
Promotion

Are you sure to start over?

Loading...